package cn.scu.ne04jdemo.controller;

import cn.scu.ne04jdemo.entitiy.RetroactiveOwnership;
import cn.scu.ne04jdemo.service.RetroactiveOwnershipService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@RestController
public class RetroactiveOwnershipController {
    private final RetroactiveOwnershipService retroactiveOwnershipService;

    public RetroactiveOwnershipController(RetroactiveOwnershipService retroactiveOwnershipService) {
        this.retroactiveOwnershipService = retroactiveOwnershipService;
    }

    @GetMapping("/retroactiveOwnership")
    public ResponseEntity<IPage> getRetroactiveOwnershipList(@RequestParam(required = false) String userName,
                                                             @RequestParam(defaultValue = "1") int pageNum,
                                                             @RequestParam(defaultValue = "10") int pageSize) {
        LambdaQueryWrapper<RetroactiveOwnership> queryWrapper = new QueryWrapper<RetroactiveOwnership>().lambda()
                .like(StringUtils.isNotBlank(userName), RetroactiveOwnership::getCUserName, userName);
        IPage<RetroactiveOwnership> page = retroactiveOwnershipService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return ResponseEntity.ok(page);
    }

    @PutMapping("/retroactiveOwnership/{id}")
    public ResponseEntity<Boolean> updateRetroactiveOwnership(@PathVariable String id,
                                                              @RequestBody RetroactiveOwnership retroactiveOwnership) {
        boolean result;
        if (retroactiveOwnership == null || !retroactiveOwnership.getId().toString().equals(id)) {
            result = false;
        } else {
            result = retroactiveOwnershipService.updateById(retroactiveOwnership);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/retroactiveOwnership/download")
    @ResponseBody
    public void download(@RequestParam String userName, HttpServletResponse response) {
        QueryWrapper<RetroactiveOwnership> params = new QueryWrapper();
        params.like(org.apache.commons.lang.StringUtils.isNotBlank(userName), "c_user_name", userName);
        List<RetroactiveOwnership> list = retroactiveOwnershipService.list(params);
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("所有权追溯.xlsx", "UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            EasyExcel.write(response.getOutputStream(), RetroactiveOwnership.class).sheet().doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
